package com.th.supcom.pds.impl.service.internal.phamapply;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import net.carefx.fc.framework.db.manager.GetSequenceHelper;
import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.translate.TranslatorManager;
import net.carefx.framework.util.ConvertUtils;

import org.omg.CORBA.OMGVMCID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import com.th.supcom.pds.domain.pojo.DeptVsDispensary;
import com.th.supcom.pds.domain.pojo.InpPhamApplyDetail;
import com.th.supcom.pds.domain.pojo.InpPhamApplyDetailSummary;
import com.th.supcom.pds.domain.pojo.InpPhamApplyMaster;
import com.th.supcom.pds.domain.pojo.InpPhamApplyPatInfo;
import com.th.supcom.pds.domain.pojo.PdsPatPhamRemain;
import com.th.supcom.pds.domain.pojo.PhamDispensaryConfig;
import com.th.supcom.pds.domain.pojo.PhamStockOnwayTable;
import com.th.supcom.pds.domain.vo.InpPhamApplyDetailSummaryVO;
import com.th.supcom.pds.domain.vo.InpPhamApplyDetailVO;
import com.th.supcom.pds.domain.vo.PhamStockOnwayTableVO;
import com.th.supcom.pds.impl.common.enums.ArreageFlagEnum;
import com.th.supcom.pds.impl.common.enums.InpChargeIndicator;
import com.th.supcom.pds.impl.common.enums.InpDispenseIndicator;
import com.th.supcom.pds.impl.common.enums.ShiftTypeEnum;
import com.th.supcom.pds.impl.common.utils.ServerUitls;
import com.th.supcom.pds.impl.manager.PhamStockOnwayTableManager;
import com.th.supcom.pds.internal.api.config.PhamDispensaryConfigService;
import com.th.supcom.pds.internal.api.phamapply.DeptVsDispensaryBasicService;
import com.th.supcom.pds.internal.api.phamapply.InpPatPhamLeftBasicService;
import com.th.supcom.pds.internal.api.phamapply.InpPhamApplyDetailBasicService;
import com.th.supcom.pds.internal.api.phamapply.InpPhamApplyInfoQueryService;
import com.th.supcom.pds.internal.api.phamapply.InpPhamApplyMasterBasicService;
import com.th.supcom.pds.internal.api.phamapply.PhamApplyService;
import com.th.supcom.pds.medicare.api.AdapterBMSChargeService;
import com.th.supcom.pds.medicare.api.AdapterOrdersService;
import com.th.supcom.pds.medicare.api.AdapterPTSPatientInfoService;

/**
 * 住院摆药请领单服务实现
 * 
 * @author 王剑
 */
public class PhamApplyServiceImpl implements PhamApplyService
{
    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (PhamApplyServiceImpl.class);
    public static final String TAB_INP_PHAM_APPLY_MASTER = "INP_PHAM_APPLY_MASTER";
    private InpPhamApplyMasterBasicService m_inpPhamApplyMasterBasicService;
    private InpPhamApplyDetailBasicService m_inpPhamApplyDetailBasicService;
    private AdapterPTSPatientInfoService m_adapterPTSPatientInfoService;
    private AdapterBMSChargeService m_adapterBMSChargeService;
    private AdapterOrdersService m_adapterOrdersService;
    private DeptVsDispensaryBasicService m_deptVsDispensaryBasicService;
    private InpPhamApplyInfoQueryService m_inpPhamApplyInfoQueryService;
    private InpPatPhamLeftBasicService m_inpPatPhamLeftBasicService;
    private TranslatorManager m_translatorManager;
    private GetSequenceHelper m_getSequenceHelper;
    private PhamDispensaryConfigService m_phamDispensaryConfigService;
    private PhamStockOnwayTableManager m_phamStockOnwayTableManager;

    public void setInpPhamApplyMasterBasicService (InpPhamApplyMasterBasicService inpPhamApplyMasterBasicService)
    {
        m_inpPhamApplyMasterBasicService = inpPhamApplyMasterBasicService;
    }

    public void setInpPhamApplyDetailBasicService (InpPhamApplyDetailBasicService inpPhamApplyDetailBasicService)
    {
        m_inpPhamApplyDetailBasicService = inpPhamApplyDetailBasicService;
    }

    public void setAdapterPTSPatientInfoService (AdapterPTSPatientInfoService adapterPTSPatientInfoService)
    {

        m_adapterPTSPatientInfoService = adapterPTSPatientInfoService;
    }

    public void setAdapterOrdersService (AdapterOrdersService adapterOrdersService)
    {

        m_adapterOrdersService = adapterOrdersService;
    }

    public void setDeptVsDispensaryBasicService (DeptVsDispensaryBasicService deptVsDispensaryBasicService)
    {

        m_deptVsDispensaryBasicService = deptVsDispensaryBasicService;
    }

    public void setInpPhamApplyInfoQueryService (InpPhamApplyInfoQueryService inpPhamApplyInfoQueryService)
    {
        m_inpPhamApplyInfoQueryService = inpPhamApplyInfoQueryService;
    }

    public void setInpPatPhamLeftBasicService (InpPatPhamLeftBasicService inpPatPhamLeftBasicService)
    {

        m_inpPatPhamLeftBasicService = inpPatPhamLeftBasicService;
    }

    public void setTranslatorManager (TranslatorManager translatorManager)
    {

        m_translatorManager = translatorManager;
    }

    public void setGetSequenceHelper (GetSequenceHelper getSequenceHelper)
    {

        m_getSequenceHelper = getSequenceHelper;
    }

    public void setAdapterBMSChargeService (AdapterBMSChargeService adapterBMSChargeService)
    {

        m_adapterBMSChargeService = adapterBMSChargeService;
    }

    public void setPhamDispensaryConfigService (PhamDispensaryConfigService phamDispensaryConfigService)
    {

        m_phamDispensaryConfigService = phamDispensaryConfigService;
    }

    @Override
    public List <InpPhamApplyDetailVO> generatePhamApplyDetailList (List <InpPhamApplyPatInfo> patInfos, InpPhamApplyMaster inpPhamApplyMaster, String repetition)
    {
        // 验证主表记录数据
        validatePhamApplyMaster (inpPhamApplyMaster);
        // 通过护理单元查找患者基本信息、费别和可用预交金
        List <InpPhamApplyPatInfo> currentPatInfos = m_adapterPTSPatientInfoService.obtainPatBasicInfo (inpPhamApplyMaster.getNurseUnit ());
        currentPatInfos = filterPatInfo (currentPatInfos, patInfos);
        // 获取预交金
        currentPatInfos = m_adapterBMSChargeService.obtainPatAvailablePrepayment (currentPatInfos);
        // 获取医嘱执行计划
        InpPhamApplyDetailVO inpPhamApplyDetailVO = new InpPhamApplyDetailVO ();
        inpPhamApplyDetailVO.setNurseUnit (inpPhamApplyMaster.getNurseUnit ());
        inpPhamApplyDetailVO.setRepetition (repetition);
        inpPhamApplyDetailVO.setBeginDateOfDay (inpPhamApplyMaster.getApplyDatetime ());
        inpPhamApplyDetailVO.setEndDateOfDay (inpPhamApplyMaster.getEndDispenseDate ());
        List <InpPhamApplyDetailVO> detailVOList = m_adapterOrdersService.findOrderExecListByExample (inpPhamApplyDetailVO);
        // 如果没有查到符合条件的记录，则直接返回
        if (CollectionUtils.isEmpty (detailVOList))
        {
            return new ArrayList <InpPhamApplyDetailVO> ();
        }
        // 获取患者住院信息, 并默认更新发送地
        detailVOList = getInpPatInfo (currentPatInfos, detailVOList, inpPhamApplyMaster.getNurseUnit ());
        if (CollectionUtils.isEmpty (detailVOList))
        {
            return new ArrayList <InpPhamApplyDetailVO> ();
        }
        // 查询药品的基本信息
        List <String> phamStdCodeList = new ArrayList <String> ();
        PhamApplyConvertUtils.convertListToMapByPhamStdCode (detailVOList, phamStdCodeList);
        InpPhamApplyDetailSummaryVO inpPhamApplyDetailSummaryVO = new InpPhamApplyDetailSummaryVO ();
        inpPhamApplyDetailSummaryVO.setPhamStdCodeList (phamStdCodeList);
        List <InpPhamApplyDetailSummary> summaryList = m_inpPhamApplyInfoQueryService.findInpApplyBasicPhamInfo (inpPhamApplyDetailSummaryVO);
        // 查询护理单元对应药房
        DeptVsDispensary condition = new DeptVsDispensary ();
        condition.setDeptCode (inpPhamApplyMaster.getNurseUnit ());
        // 查询护理单元与药房的配置关系
        List <DeptVsDispensary> deptVsDispensaryList = m_deptVsDispensaryBasicService.findDeptVsDispensaryListByNurseCode (condition);
        if (CollectionUtils.isEmpty (deptVsDispensaryList))
        {
            s_logger.error ("没有配置护理单元与药房的对应关系!");
            throw new BusinessException ("没有配置护理单元与药房的对应关系!");
        }
        // 查询摆药类型与药房的配置关系
        condition.setDeptCode (null);
        List <DeptVsDispensary> dipsenseTypeVsDispensaryList = m_deptVsDispensaryBasicService.findDispenseTypeListByExample (condition);
        // 查询药品材质与药房的配置关系
        List <DeptVsDispensary> phamMaterialVsDispensaryList = m_deptVsDispensaryBasicService.findPhamMaterialTypeListByExample (condition);
        // 根据班次区分药房
        Map <String, List <DeptVsDispensary>> shiftDispensaryMap = divisiveDispensaryByShift (deptVsDispensaryList);
        // 判断是否是夜班药房
        boolean isNight = isNightShift (inpPhamApplyMaster.getNurseUnit (), shiftDispensaryMap);
        // 指定药房品的发药药房,同时制定summaryList中药品的发药药房
        AppointDispensary.distributeDispensary (detailVOList, deptVsDispensaryList, shiftDispensaryMap, dipsenseTypeVsDispensaryList, phamMaterialVsDispensaryList, summaryList, isNight);
        // 查询药品在各个发药药房的库存以及可供信息
        List <InpPhamApplyDetailSummary> phamStockList = m_inpPhamApplyInfoQueryService.findInpApplyPhamStockInfo (summaryList);
        PhamApplyStaticMethod.getInpApplyPhamStockInfo (summaryList, phamStockList);
        // 查询患者的剩余数量
        List <Long> paivisitIdList = new ArrayList <Long> ();
        Map <Long, List <InpPhamApplyDetailVO>> detailListMap = PhamApplyConvertUtils.convertListToMapByPaivisitID (detailVOList, paivisitIdList);
        List <PdsPatPhamRemain> lefts = m_inpPatPhamLeftBasicService.findPatPhamRemainsByPaiVisitIdList (paivisitIdList);
        // 计算住院患者药品的数量和费用
        CalculatePhamAmountAndCost.getDispenseCount (lefts, detailListMap, summaryList, isNight);
        // 根据医嘱子序号合并
        detailVOList = PhamApplyStaticMethod.combineDetailByOrderSubNo (detailVOList);
        // 标识患者是否欠费
        checkArreageStatus (currentPatInfos, detailListMap);
        // 更新请领明细药品的库存状态
        PhamApplyStaticMethod.updateApplyStockStatus (detailVOList, summaryList);
        // 更新是否摆药
        PhamApplyStaticMethod.updateApplyDispenseFlag (currentPatInfos, detailVOList);
        return m_translatorManager.translate (PhamApplyStaticMethod.combineDetailByOrderSubNo (detailVOList));
    }

    /**
     * 根据班次获取药房
     * 
     * @return
     */
    private static Map <String, List <DeptVsDispensary>> divisiveDispensaryByShift (List <DeptVsDispensary> deptVsDispensaryList)
    {
        Map <String, List <DeptVsDispensary>> map = new HashMap <String, List <DeptVsDispensary>> ();
        List <DeptVsDispensary> deptVsDispensaries = null;
        for (DeptVsDispensary dispensary : deptVsDispensaryList)
        {
            if (ShiftTypeEnum.NightShift.getEnumCode ().equals (dispensary.getShift ()))
            {
                deptVsDispensaries = map.get (ShiftTypeEnum.NightShift.getEnumCode ());
            }
            else
            {
                deptVsDispensaries = map.get (ShiftTypeEnum.DayShift.getEnumCode ());
            }

            if (null == deptVsDispensaries)
            {
                deptVsDispensaries = new ArrayList <DeptVsDispensary> ();
            }

            deptVsDispensaries.add (dispensary);

            map.put (dispensary.getShift (), deptVsDispensaries);
        }
        return map;
    }

    /**
     * 过滤得到选中的患者
     * 
     * @param currentPatInfos 护理单元中所有的患者信息
     * @param patInfos 当前选中的患者
     * @return
     */
    private List <InpPhamApplyPatInfo> filterPatInfo (List <InpPhamApplyPatInfo> currentPatInfos, List <InpPhamApplyPatInfo> patInfos)
    {
        Map <Long, InpPhamApplyPatInfo> map = new HashMap <Long, InpPhamApplyPatInfo> ();
        List <InpPhamApplyPatInfo> currentApplyPatInfos = new ArrayList <InpPhamApplyPatInfo> ();
        for (InpPhamApplyPatInfo patInfo : patInfos)
        {
            if (!map.containsKey (patInfo.getPaiVisitId ()))
            {
                map.put (patInfo.getPaiVisitId (), patInfo);
            }
        }

        for (InpPhamApplyPatInfo patInfo : currentPatInfos)
        {
            if (map.containsKey (patInfo.getPaiVisitId ()))
            {
                patInfo.setArreageDispense (map.get (patInfo.getPaiVisitId ()).getArreageDispense ());
                currentApplyPatInfos.add (patInfo);
            }
        }
        return currentApplyPatInfos;
    }

    @Override
    public void savePhamApplyMasterAndDetail (InpPhamApplyMaster inpPhamApplyMaster, List <InpPhamApplyDetailVO> detailList)
    {
        s_logger.info ("调用savePhamApplyMasterAndDetail参数 ：inpPhamApplyMaster:{},detailList{}", new Object[]
        { inpPhamApplyMaster, detailList });
        if (null == inpPhamApplyMaster)
        {
            s_logger.error ("保存住院摆药请领主表和明细时，请领单据不能为空");
            throw new IllegalArgumentException ("保存住院摆药请领主表和明细时，请领单据不能为空");
        }
        if (detailList.isEmpty ())
        {
            s_logger.error ("保存住院摆药请领主表和明细时，请领单据明细不能为空");
            throw new IllegalArgumentException ("保存住院摆药请领主表和明细时，请领单据明细不能为空");
        }

        String applyNoString = PhamApplyStaticMethod.generateApplyNo (m_getSequenceHelper.getNextSequenceId (TAB_INP_PHAM_APPLY_MASTER), inpPhamApplyMaster.getApplyBy ());
        inpPhamApplyMaster.setApplyNo (applyNoString);
        m_inpPhamApplyMasterBasicService.addInpPhamApplyMaster (inpPhamApplyMaster);
        List <InpPhamApplyDetail> saveList;
        for (InpPhamApplyDetailVO inpPhamApplyDetailVO : detailList)
        {
            inpPhamApplyDetailVO.setApplyNo (inpPhamApplyMaster.getApplyNo ());
            inpPhamApplyDetailVO.setDispenseIndicator (InpDispenseIndicator.Supplyed.getEnumCode ());
            inpPhamApplyDetailVO.setChargeIndicator (InpChargeIndicator.Uncharged.getEnumCode ());
        }
        saveList = ConvertUtils.convert (InpPhamApplyDetail.class, detailList);
        m_inpPhamApplyDetailBasicService.addInpPhamApplyDetailList (saveList);
    }

    /**
     * 验证请领主记录
     */
    private void validatePhamApplyMaster (InpPhamApplyMaster master)
    {
        if (null == master)
        {
            throw new BusinessException ("生成请领明细时，主记录对象不能为空");
        }

        if (null == master.getNurseUnit ())
        {
            throw new BusinessException ("生成请领明细时，护理单元不能为空");
        }

        if (null == master.getEndDispenseDate ())
        {
            throw new BusinessException ("生成请领明细时，摆药截止时间不能为空");
        }

        if (null == master.getApplyBy ())
        {
            throw new BusinessException ("生成请领明细时，申请人不能为空");
        }
    }

    @Override
    public List <InpPhamApplyPatInfo> getApplyPatInfosByNurseUnit (String nurseUnitCode)
    {
        // 查询获取护理单元中的在院患者
        List <InpPhamApplyPatInfo> inpPhamApplyPatInfos = m_adapterPTSPatientInfoService.obtainPatBasicInfo (nurseUnitCode);
        if (CollectionUtils.isEmpty (inpPhamApplyPatInfos))
        {
            return new ArrayList <InpPhamApplyPatInfo> ();
        }
        // 根据护理单元查询对应的药房
        DeptVsDispensary deptVsDispensary = new DeptVsDispensary ();
        deptVsDispensary.setDeptCode (nurseUnitCode);
        boolean isArreage = true;
        List <DeptVsDispensary> deptVsDispensaries = m_deptVsDispensaryBasicService.findDeptVsDispensaryListByNurseCode (deptVsDispensary);

        // 如果配置关系为空则默认为欠费摆药
        if (!CollectionUtils.isEmpty (deptVsDispensaries) && ArreageFlagEnum.NO.getEnumCode ().equals (deptVsDispensaries.get (0).getIsArrear ()))
        {
            isArreage = false;
        }

        if (!isArreage)
        {
            for (InpPhamApplyPatInfo patInfo : inpPhamApplyPatInfos)
            {
                patInfo.setArreageDispense (String.valueOf (ArreageFlagEnum.NO.getEnumCode ()));
            }
        }
        return m_translatorManager.translate (inpPhamApplyPatInfos);
    }

    /**
     * 验证本次摆药患者是否欠费
     * 
     * @param patInfos
     * @param inpPhamApplyDetailVOs
     */
    private void checkArreageStatus (List <InpPhamApplyPatInfo> patInfos, Map <Long, List <InpPhamApplyDetailVO>> detailMap)
    {
        if (CollectionUtils.isEmpty (patInfos))
        {
            return;
        }

        for (InpPhamApplyPatInfo patInfo : patInfos)
        {
            checkArreageStatusByPatient (patInfo, detailMap);
        }
    }

    /**
     * 验证单个患者本次摆药是否欠费
     * 
     * @param patInfo
     * @param inpPhamApplyDetailVOs
     */
    private void checkArreageStatusByPatient (InpPhamApplyPatInfo patInfo, Map <Long, List <InpPhamApplyDetailVO>> detailMap)
    {
        double sumCost = 0.0;

        List <InpPhamApplyDetailVO> detailVOs = detailMap.get (patInfo.getPaiVisitId ());

        if (CollectionUtils.isEmpty (detailVOs))
        {
            return;
        }

        for (InpPhamApplyDetailVO vo : detailVOs)
        {
            sumCost += vo.getCosts ();
        }

        if (null == patInfo.getAvailablePrepayment ())
        {
            patInfo.setAvailablePrepayment (0D);
        }

        if (sumCost > patInfo.getAvailablePrepayment ())
        {
            updateArreageStatusByPatient (detailVOs, true);
        }
        else
        {
            updateArreageStatusByPatient (detailVOs, false);
        }

    }

    /**
     * 更新预交金和欠费信息
     * 
     * @param detailVOList
     * @param arreage
     * @param balance
     */
    private void updateArreageStatusByPatient (List <InpPhamApplyDetailVO> detailVOList, boolean arreage)
    {
        for (InpPhamApplyDetailVO vo : detailVOList)
        {
            vo.setArreas (arreage);
        }
    }

    @Override
    public void saveInpPhamApplyDetailList (List <InpPhamApplyDetail> details)
    {
        m_inpPhamApplyDetailBasicService.addInpPhamApplyDetailList (details);
    }

    /**
     * 更新患者住院信息和默认药品发往地
     */
    private List <InpPhamApplyDetailVO> getInpPatInfo (List <InpPhamApplyPatInfo> patInfos, List <InpPhamApplyDetailVO> detailVOList, String nurseUnit)
    {
        List <InpPhamApplyDetailVO> tempList = new ArrayList <InpPhamApplyDetailVO> ();
        Map <Long, InpPhamApplyPatInfo> patMap = PhamApplyConvertUtils.convertPatInfoListToMapByPaivisitId (patInfos);
        for (InpPhamApplyDetailVO vo : detailVOList)
        {
            InpPhamApplyPatInfo patInfo = patMap.get (vo.getPaiVisitId ());
            if (null == patInfo || null == patInfo.getBedNo ())
            {
                continue;
            }
            vo.setBedNo (patInfo.getBedNo ());
            vo.setPaiName (patInfo.getPatName ());
            vo.setGender (patInfo.getGender ());
            vo.setMandatoryDispenseFlag (patInfo.getArreageDispense ());
            vo.setChargeType (patInfo.getChargeType ());
            vo.setNurseUnit (nurseUnit);
            vo.setDestination (nurseUnit);
            vo.setBalance (patInfo.getAvailablePrepayment ());
            tempList.add (vo);
        }

        return tempList;
    }

    @Override
    public List <InpPhamApplyDetailVO> getOffsetPhamSummaryList (List <InpPhamApplyDetailSummary> summaryList)
    {

        if (CollectionUtils.isEmpty (summaryList))
        {
            return new ArrayList <InpPhamApplyDetailVO> ();
        }
        List <InpPhamApplyDetailVO> inpPhamApplyDetailVOs = new ArrayList <InpPhamApplyDetailVO> ();
        Map <String, InpPhamApplyDetailVO> map = new HashMap <String, InpPhamApplyDetailVO> ();
        List <PhamStockOnwayTableVO> phamStockOnwayTables = m_phamStockOnwayTableManager.findOffsetPhamList (summaryList);
        for (PhamStockOnwayTableVO phamStockOnwayTableVO : phamStockOnwayTables)
        {
            if (!map.containsKey (phamStockOnwayTableVO.getPhamStdCode ()))
            {
                InpPhamApplyDetailVO inpPhamApplyDetailVO = new InpPhamApplyDetailVO ();
                inpPhamApplyDetailVO.setPhamStdCode (phamStockOnwayTableVO.getPhamStdCode ());
                inpPhamApplyDetailVO.setApplyUnit (phamStockOnwayTableVO.getPackQuantityUnit ());
                inpPhamApplyDetailVO.setExecDispensary (phamStockOnwayTableVO.getDeptCode ());
                inpPhamApplyDetailVO.setPhamName (phamStockOnwayTableVO.getPhamName ());
                inpPhamApplyDetailVO.setPhamSpec (phamStockOnwayTableVO.getPhamSpec ());
                inpPhamApplyDetailVO.setOffsetSum (phamStockOnwayTableVO.getQuantity ().intValue ());
                inpPhamApplyDetailVO.setStockNo (phamStockOnwayTableVO.getInoutNumber ());
                inpPhamApplyDetailVO.setNurseUnit (phamStockOnwayTableVO.getDeliverOrg ());
                map.put (phamStockOnwayTableVO.getPhamStdCode (), inpPhamApplyDetailVO);
            }
            else
            {
                InpPhamApplyDetailVO inpPhamApplyDetailVO = map.get (phamStockOnwayTableVO.getPhamStdCode ());
                inpPhamApplyDetailVO.setOffsetSum (inpPhamApplyDetailVO.getOffsetSum () + phamStockOnwayTableVO.getQuantity ().intValue ());
                map.put (phamStockOnwayTableVO.getPhamStdCode (), inpPhamApplyDetailVO);
            }
        }
        Set <Entry <String, InpPhamApplyDetailVO>> set = map.entrySet ();
        for (Entry <String, InpPhamApplyDetailVO> entry : set)
        {
            inpPhamApplyDetailVOs.add (entry.getValue ());
        }
        return m_translatorManager.translate (inpPhamApplyDetailVOs);
    }

    @Override
    public List <InpPhamApplyDetailVO> getOffsetPhamList (List <InpPhamApplyDetailSummary> summaryList)
    {
        if (CollectionUtils.isEmpty (summaryList))
        {
            return new ArrayList <InpPhamApplyDetailVO> ();
        }
        List <InpPhamApplyDetailVO> inpPhamApplyDetailVOs = new ArrayList <InpPhamApplyDetailVO> ();
        List <PhamStockOnwayTableVO> phamStockOnwayTables = m_phamStockOnwayTableManager.findOffsetPhamList (summaryList);
        for (PhamStockOnwayTableVO phamStockOnwayTableVO : phamStockOnwayTables)
        {
            InpPhamApplyDetailVO inpPhamApplyDetailVO = new InpPhamApplyDetailVO();
            inpPhamApplyDetailVO.setPhamStdCode (phamStockOnwayTableVO.getPhamStdCode ());
            inpPhamApplyDetailVO.setStockNo (phamStockOnwayTableVO.getInoutNumber ());
            inpPhamApplyDetailVO.setOffsetAmount (phamStockOnwayTableVO.getQuantity ());
            inpPhamApplyDetailVO.setExecDispensary (phamStockOnwayTableVO.getDeptCode ());
            inpPhamApplyDetailVOs.add (inpPhamApplyDetailVO);
        }
        return m_translatorManager.translate (inpPhamApplyDetailVOs);
    }

    @Override
    public boolean isNightShift (String nurseUnit, Map <String, List <DeptVsDispensary>> shiftDispensaryMap)
    {
        // 获取夜班药房配置
        List <DeptVsDispensary> nightDeptVsDispensaries = null;
        nightDeptVsDispensaries = shiftDispensaryMap.get (ShiftTypeEnum.NightShift.getEnumCode ());
        if (CollectionUtils.isEmpty (nightDeptVsDispensaries))
        {
            s_logger.error ("护理单元没有配置夜班药房");
            throw new BusinessException ("护理单元没有配置夜班药房");
        }

        if (nightDeptVsDispensaries.size () > 1)
        {
            s_logger.error ("护理单元只能配置唯一的夜班药房");
            throw new BusinessException ("护理单元只能配置唯一的夜班药房");
        }

        DeptVsDispensary deptVsDispensary = nightDeptVsDispensaries.get (0);

        PhamDispensaryConfig phamDispensaryConfig = new PhamDispensaryConfig ();
        phamDispensaryConfig.setShiftType (ShiftTypeEnum.NightShift.getEnumCode ());
        phamDispensaryConfig.setPhamDispensaryCode (deptVsDispensary.getDispensary ());
        List <PhamDispensaryConfig> configs = m_phamDispensaryConfigService.findPhamDispensaryConfigByExample (phamDispensaryConfig);
        if (CollectionUtils.isEmpty (configs))
        {
            return false;
        }

        PhamDispensaryConfig config = configs.get (0);
        if (ServerUitls.isNight (config.getStartTime ()))
        {
            return true;
        }

        return false;

    }

    public void setPhamStockOnwayTableManager (PhamStockOnwayTableManager phamStockOnwayTableManager)
    {
        m_phamStockOnwayTableManager = phamStockOnwayTableManager;
    }

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

}
